var organizationItemProductionLine_table;
$(function(){
	organizationItemProductionLine_table = $('#organizationItemProductionLine_table_id');
	var add_organizationItemProductionLine_item = $("#add_organizationItemProductionLine_item");
	var up_organizationItemProductionLine_item = $("#up_organizationItemProductionLine_item");
	var select_organizationItemProductionLine_item = $("#select_organizationItemProductionLine_item");

	  $.ajax({
			type : 'POST',
			url : '/getAllOrganizationItem',
			async : false,
			success : function(data) {
				add_organizationItemProductionLine_item.empty();
				up_organizationItemProductionLine_item.empty();
				select_organizationItemProductionLine_item.empty();
				$.each(data, function(i, item) {
						add_organizationItemProductionLine_item.append($('<option value="' + item.id + '">'
								+ item.name + '</option>'));
						up_organizationItemProductionLine_item.append($('<option value="' + item.id + '">'
								+ item.name + '</option>'));
						select_organizationItemProductionLine_item.append($('<option value="' + item.id + '">'
								+ item.name + '</option>'));
				});
				add_organizationItemProductionLine_item.select2();
				up_organizationItemProductionLine_item.select2();
				select_organizationItemProductionLine_item.select2();
			}
		});
	organizationItemProductionLine_table.bootstrapTable({
		url : "/getOrganizationItemProductionLinePage",
		striped : true, // 是否显示行间隔色

		search : true,

		// 分页相关
		sidePagination : 'server',
		pagination : true, // 是否显示分页（*）
		pageNumber : 1, // 初始化加载第一页，默认第一页,并记录
		pageSize : 10, // 默认显示几条数据 
		pageList : [ 10, 50, 100 ], // 用于设置当前页面可以显示的数据条数
		queryParams : function(params) {
			var nparams = {
				'itemId' : $('#select_organizationItemProductionLine_item').val(),
				'search' : params.search,
				'pageNumber' : (params.offset/params.limit)+1,
				'pageSize' : params.limit
			};
			return nparams;
		},
		showRefresh : true, // 是否显示刷新按钮
		uniqueId : 'id',
		columns : [ {
			checkbox : true,
			visible : true
		// 是否显示复选框 
		}, {
			field : 'id',
			title : '生产线编号',
			width : "20%"
		}, {
			field : 'name',
			title : '生产线名称',
			width : "20%"
		}, {
			field : 'item.name',
			title : '所属商品',
			width : "20%"
		}, {
			field : 'item.organization.name',
			title : '所属机构',
			width : "40%"
		}]
	});
	
	select_organizationItemProductionLine_item.on('select2:select',function(){
		organizationItemProductionLine_table.bootstrapTable('refresh',{pageNumber:1});
	});
	
	var add_organizationItemProductionLine_btn = $("#add_organizationItemProductionLine_btn_id");
	add_organizationItemProductionLine_btn.click(function() {
		$('#add_organizationItemProductionLine_Modal').modal("show");
	});
	var up_organizationItemProductionLine_btn = $("#up_organizationItemProductionLine_btn_id");
	up_organizationItemProductionLine_btn.click(function() {
		var selectRows = organizationItemProductionLine_table.bootstrapTable("getSelections");
		if (selectRows.length != 1) {
			BootstrapDialog.show({
				type : BootstrapDialog.TYPE_WARNING,
				title : "错误",
				buttons : [ {
					label : '关闭',
					action : function(dialogItself) {
						dialogItself.close();
					}
				} ],
				message : '请选择一行记录'
			});
			return;
		}
		$('#up_organizationItemProductionLine_id').val(selectRows[0].id);
		$('#up_organizationItemProductionLine_name').val(selectRows[0].name);
		$('#up_organizationItemProductionLine_item').val(selectRows[0].item.id).select2();

		$('#up_organizationItemProductionLine_Modal').modal("show");
	});
	var add_organizationItemProductionLine_submit_btn = $("#add_organizationItemProductionLine_submit_btn_id");
	add_organizationItemProductionLine_submit_btn.click(function() {
		$('#add_organizationItemProductionLine_Modal').modal("hide");
		var formArray = getFormDataArray($("#add_organizationItemProductionLine_form_id"));

		var formData = eval('({' + formArray.join(',') + '})');
		$.ajax({
			type : 'POST',
			url : '/addOrganizationItemProductionLine',
			data : formData,
			success : function(data) {
				var type;
				if (data.status == 0) {
					organizationItemProductionLine_table.bootstrapTable("refresh");
					type = BootstrapDialog.TYPE_SUCCESS;
				} else if (data == 1) {
					type = BootstrapDialog.TYPE_WARNING;
				} else {
					type = BootstrapDialog.TYPE_DANGER;
				}
				BootstrapDialog.show({
					type : type,
					title : "执行结果",
					buttons : [ {
						label : '关闭',
						action : function(dialogItself) {
							dialogItself.close();
						}
					} ],
					message : data.msg
				});
			}
		});
	});
	var up_organizationItemProductionLine_submit_btn = $("#up_organizationItemProductionLine_submit_btn_id");
	up_organizationItemProductionLine_submit_btn.click(function() {
		$('#up_organizationItemProductionLine_Modal').modal("hide");
		var formArray = getFormDataArray($("#up_organizationItemProductionLine_form_id"));

		var formData = eval('({' + formArray.join(',') + '})');
		$.ajax({
			type : 'POST',
			url : '/upOrganizationItemProductionLine',
			data : formData,
			success : function(data) {
				var type;
				if (data.status == 0) {
					organizationItemProductionLine_table.bootstrapTable("refresh");
					type = BootstrapDialog.TYPE_SUCCESS;
				} else if (data == 1) {
					type = BootstrapDialog.TYPE_WARNING;
				} else {
					type = BootstrapDialog.TYPE_DANGER;
				}
				BootstrapDialog.show({
					type : type,
					title : "执行结果",
					buttons : [ {
						label : '关闭',
						action : function(dialogItself) {
							dialogItself.close();
						}
					} ],
					message : data.msg
				});
			}
		});

	});
	var del_organizationItemProductionLine_btn = $("#del_organizationItemProductionLine_btn_id");
	del_organizationItemProductionLine_btn.click(function() {
		var selectRows = organizationItemProductionLine_table.bootstrapTable("getSelections");
		if (selectRows.length == 0) {
			BootstrapDialog.show({
				type : BootstrapDialog.TYPE_WARNING,
				title : "错误",
				buttons : [ {
					label : '关闭',
					action : function(dialogItself) {
						dialogItself.close();
					}
				} ],
				message : '至少选择一行记录'
			});
			return;
		} else {
			var ids = [];
			$.each(selectRows, function(i, row) {
				ids.push(row.id);
			});

			$.ajax({
				type : 'POST',
				url : '/delOrganizationItemProductionLine',
				data : {
					"ids" : ids.join(",")
				},
				success : function(data) {
					var type;
					if (data.status == 0) {
						organizationItemProductionLine_table.bootstrapTable("refresh");
						type = BootstrapDialog.TYPE_SUCCESS;
					} else if (data == 1) {
						type = BootstrapDialog.TYPE_WARNING;
					} else {
						type = BootstrapDialog.TYPE_DANGER;
					}
					BootstrapDialog.show({
						type : type,
						title : "执行结果",
						buttons : [ {
							label : '关闭',
							action : function(dialogItself) {
								dialogItself.close();
							}
						} ],
						message : data.msg
					});
				}
			});
		}
	});

});
